﻿using System.Data;
using System;

namespace eDoc.DAL
{
    /// <summary>
    /// 
    /// </summary>
    public class StoredProcedures : StoredProcedures_Base
    {

        private MainDB _db;

        public StoredProcedures(MainDB db)
            : base(db)
        {
            _db = db;
        }
        

        /// <summary>
        /// Insert thong tin cua ung dung
        /// </summary>
        /// <param name="AppKey">Key tu sinh cua ung dung</param>
        /// <param name="AppName">Ten ung dung</param>
        /// <param name="AppVersion">Phien ban cua ung dung</param>
        /// <param name="AppBundleIdentifier">Bundle Identifier</param>
        /// <param name="AppDeviceFamily">Device Family</param>
        /// <param name="AppMinimumOSVersion">Minimum OS Version</param>
        /// <param name="AppSize">Size</param>
        /// <param name="AppPlistPath">Duong dan file plist</param>
        /// <param name="AppUrlInstall">Duong dan file ipa de install</param>
        public void VNA_Insert_AppPlist(string AppKey, string AppName, string AppVersion, string AppBundleIdentifier, string AppDeviceFamily, string AppMinimumOSVersion, int AppSize, string AppPlistPath, string AppUrlInstall)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Insert_AppPlist", true);
            _db.AddParameter(cmd, "AppKey", DbType.String, AppKey);
            _db.AddParameter(cmd, "AppName", DbType.String, AppName);
            _db.AddParameter(cmd, "AppVersion", DbType.String, AppVersion);
            _db.AddParameter(cmd, "AppBundleIdentifier", DbType.String, AppBundleIdentifier);
            _db.AddParameter(cmd, "AppDeviceFamily", DbType.String, AppDeviceFamily);
            _db.AddParameter(cmd, "AppMinimumOSVersion", DbType.String, AppMinimumOSVersion);
            _db.AddParameter(cmd, "AppSize", DbType.Int32, AppSize);
            _db.AddParameter(cmd, "AppPlistPath", DbType.String, AppPlistPath);
            _db.AddParameter(cmd, "AppUrlInstall", DbType.String, AppUrlInstall);

            CreateDataTable(cmd);
           
        }

        /// <summary>
        /// Lay danh sach cac ung dung
        /// </summary>
        /// <returns></returns>
        public DataTable VNA_Select_AppPlist()
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Select_AppPlist", true);
            DataTable dt = CreateDataTable(cmd);
            return dt;
        }

        /// <summary>
        /// Dang nhap de tai ung dung
        /// </summary>
        /// <param name="Username">Ten dang nhap</param>
        /// <param name="Password">Mat khau</param>
        /// <returns></returns>
        public DataTable VNA_LoginDownload(string Username, string Password)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_LoginDownload", true);
            _db.AddParameter(cmd, "Username", DbType.String, Username);
            _db.AddParameter(cmd, "Password", DbType.String, Password);
            DataTable dt = CreateDataTable(cmd);
            return dt;
        }

        /// <summary>
        /// Lay thong tin file theo id
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public DataTable VNA_Select_AppPlist_by_ID(int ID)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Select_AppPlist_by_ID", true);
            _db.AddParameter(cmd, "ID", DbType.Int32, ID);     
            DataTable dt = CreateDataTable(cmd);
            return dt;
        }

        /// <summary>
        /// Lay thong tin file theo AppKey
        /// </summary>
        /// <param name="AppKey"></param>
        /// <returns></returns>
        public DataTable VNA_Select_AppPlist_by_AppKey(string AppKey)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Select_AppPlist_by_AppKey", true);
            _db.AddParameter(cmd, "AppKey", DbType.String, AppKey);
            DataTable dt = CreateDataTable(cmd);
            return dt;
        }


        /// <summary>
        /// Kiem tra login tren ung dung
        /// </summary>
        /// <param name="Username"></param>
        /// <param name="Password"></param>
        /// <param name="Udid"></param>
        /// <param name="PartnerCode"></param>
        /// <returns></returns>
        public DataTable VNA_Login_App(string Username, string Password, string Udid, string PartnerCode)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Login_App", true);
            _db.AddParameter(cmd, "Username", DbType.String, Username);
            _db.AddParameter(cmd, "Password", DbType.String, Password);
            _db.AddParameter(cmd, "Udid", DbType.String, Udid);
            _db.AddParameter(cmd, "PartnerCode", DbType.String, PartnerCode);
            DataTable dt = CreateDataTable(cmd);
            return dt;
        }

        /// <summary>
        /// Active User voi Udid
        /// </summary>
        /// <param name="Username"></param>
        /// <param name="Password"></param>
        /// <param name="Udid"></param>
        /// <param name="PartnerCode"></param>
        /// <returns></returns>
        public void VNA_Active(string Username, string Password, string Udid, string PartnerCode)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Active", true);
            _db.AddParameter(cmd, "Username", DbType.String, Username);
            _db.AddParameter(cmd, "Password", DbType.String, Password);
            _db.AddParameter(cmd, "Udid", DbType.String, Udid);
            _db.AddParameter(cmd, "PartnerCode", DbType.String, PartnerCode);
            CreateDataTable(cmd);
            
        }

        /// <summary>
        /// Lay thong tin server theo Udid cua may
        /// </summary>
        /// <param name="Udid"></param>
        /// <param name="PartnerCode"></param>
        public DataTable VNA_Select_FTP_by_Udid(string Udid, string PartnerCode)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Select_FTP_by_Udid", true);
            _db.AddParameter(cmd, "Udid", DbType.String, Udid);
            _db.AddParameter(cmd, "PartnerCode", DbType.String, PartnerCode);          
            DataTable dt = CreateDataTable(cmd);

            return dt;
        }

        /// <summary>
        /// Lay danh sach FTP theo partnercode
        /// </summary>
        /// <param name="PartnerCode"></param>
        /// <returns></returns>
        public DataTable VNA_Select_FTP_by_PartnerCode(string PartnerCode)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Select_FTP_by_PartnerCode", true);            
            _db.AddParameter(cmd, "PartnerCode", DbType.String, PartnerCode);
            DataTable dt = CreateDataTable(cmd);

            return dt;
        }

        /// <summary>
        /// Lay danh sach FTP theo partnercode va Type : Type = 0 : server document; Type = 1 : server veryfi
        /// </summary>
        /// <param name="PartnerCode"></param>
        /// <param name="Type"></param>
        /// <returns></returns>
        public DataTable VNA_Select_FTP_by_PartnerCode_Type(string PartnerCode, int Type)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Select_FTP_by_PartnerCode_Type", true);
            _db.AddParameter(cmd, "PartnerCode", DbType.String, PartnerCode);
            _db.AddParameter(cmd, "Type", DbType.Int32, Type);
            DataTable dt = CreateDataTable(cmd);

            return dt;
        }

        /// <summary>
        /// Danh sach thong tin App theo AppID
        /// </summary>
        /// <param name="AppID"></param>
        /// <returns></returns>
        public DataTable VNA_Select_AppPlist_by_AppID(string AppID)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Select_AppPlist_by_AppID", true);
            _db.AddParameter(cmd, "AppID", DbType.String, AppID);
           
            DataTable dt = CreateDataTable(cmd);

            return dt;
        }


        /// <summary>
        /// Kiem tra xem Uuid nay co trong blacklist hay ko ?
        /// </summary>
        /// <param name="Uuid"></param>
        /// <returns></returns>
        public DataTable VNA_Select_Blacklist_by_Uuid(string Uuid)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Select_Blacklist_by_Uuid", true);
            _db.AddParameter(cmd, "Uuid", DbType.String, Uuid);

            DataTable dt = CreateDataTable(cmd);

            return dt;
        }


        /// <summary>
        /// Them moi Uuid vao blacklist theo Uuid va AppID
        /// </summary>
        /// <param name="uuid"></param>
        /// <param name="appId"></param>
        /// <param name="note"></param>
        /// <returns></returns>
        public DataTable VNA_Insert_Blacklist(string uuid, string appId, string note)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Insert_Blacklist", true);
            _db.AddParameter(cmd, "Uuid", DbType.String, uuid);
            _db.AddParameter(cmd, "AppID", DbType.String, appId);
            _db.AddParameter(cmd, "Note", DbType.String, note);

            DataTable dt = CreateDataTable(cmd);

            return dt;
        }

        /// <summary>
        /// Xoa blacklist theo Uuid va AppID
        /// </summary>
        /// <param name="uuid"></param>
        /// <param name="appId"></param>
        public DataTable VNA_Delete_Blacklist_by_Uuid_and_AppID(string uuid, string appId)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Delete_Blacklist_by_Uuid_and_AppID", true);
            _db.AddParameter(cmd, "Uuid", DbType.String, uuid);
            _db.AddParameter(cmd, "AppID", DbType.String, appId);            

            return CreateDataTable(cmd);

            
        }


        /// <summary>
        /// Xoa Blacklist theo Uuid
        /// </summary>
        /// <param name="uuid"></param>
        public DataTable VNA_Delete_Blacklist_by_Uuid(string uuid)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Delete_Blacklist_by_Uuid", true);
            _db.AddParameter(cmd, "Uuid", DbType.String, uuid);           

            return CreateDataTable(cmd);


        }

       
        /// <summary>
        ///  Ghi lai log Sync du lieu
        /// </summary>
        /// <param name="Uuid"></param>
        /// <param name="Email"></param>
        /// <param name="Content"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="documentAttributes"></param>
        /// <param name="appId"></param>
        /// <param name="iPadName"></param>
        /// <param name="buildMode"></param>
        /// <param name="msisdn"></param>
        /// <param name="carrier"></param>
        /// <param name="manufacture"></param>
        /// <param name="filesCount"></param>
        public void VNA_Insert_LogSync(string Uuid, string Email, string Content, int ErrorCode, string documentAttributes, string appId, string iPadName, string buildMode, string msisdn, string carrier, string manufacture, int filesCount)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Insert_LogSync", true);
            _db.AddParameter(cmd, "Uuid", DbType.String, Uuid);
            _db.AddParameter(cmd, "Email", DbType.String, Email);
            _db.AddParameter(cmd, "Content", DbType.String, Content);
            _db.AddParameter(cmd, "ErrorCode", DbType.Int32, ErrorCode);
            _db.AddParameter(cmd, "documentAttributes", DbType.String, documentAttributes);
            _db.AddParameter(cmd, "appId", DbType.String, appId);
            _db.AddParameter(cmd, "iPadName", DbType.String, iPadName);
            _db.AddParameter(cmd, "buildMode", DbType.String, buildMode);
            _db.AddParameter(cmd, "msisdn", DbType.String, msisdn);
            _db.AddParameter(cmd, "carrier", DbType.String, carrier);
            _db.AddParameter(cmd, "manufacture", DbType.String, manufacture);
            _db.AddParameter(cmd, "filesCount", DbType.Int32, filesCount);
            
            CreateDataTable(cmd);
        }

        /// <summary>
        /// Luu lại thông tin của thiết bị và người dùng khi kích hoạt ứng dụng
        /// </summary>
        /// <param name="Uuid"></param>
        /// <param name="Email"></param>
        /// <param name="Password"></param>
        /// <param name="Active"></param>
        /// <param name="AppID"></param>
        public DataTable VNA_Insert_Devices(string Uuid, string Email, string Password, bool Active, string AppID, string Push_ID, string iPadName)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Insert_Devices", true);
            _db.AddParameter(cmd, "Uuid", DbType.String, Uuid);
            _db.AddParameter(cmd, "Email", DbType.String, Email);
            _db.AddParameter(cmd, "Password", DbType.String, Password);
            _db.AddParameter(cmd, "Active", DbType.Boolean, Active);
            _db.AddParameter(cmd, "AppID", DbType.String, AppID);
            _db.AddParameter(cmd, "Push_ID", DbType.String, Push_ID);
            _db.AddParameter(cmd, "iPadName", DbType.String, iPadName);
            DataTable dt = CreateDataTable(cmd);
            return dt;
        }

        /// <summary>
        /// Lấy danh sách devices được active theo Email của người dùng (Mỗi email được tối đa 2 thiết bị active)
        /// </summary>
        /// <param name="Email"></param>
        /// <returns></returns>
        public DataTable VNA_Select_Devices_by_Email(string Email)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Select_Devices_by_Email", true);
            _db.AddParameter(cmd, "Email", DbType.String, Email);
            DataTable dt = CreateDataTable(cmd);
            return dt;
        }

        /// <summary>
        /// Lấy danh sách devices được active theo Email người dùng, Uuid cua may
        /// </summary>
        /// <param name="Email"></param>
        /// <param name="Uuid"></param>
        /// <returns></returns>
        public DataTable VNA_Select_Devices_by_Email_Uuid(string Email, string Uuid)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Select_Devices_by_Email_Uuid", true);
            _db.AddParameter(cmd, "Email", DbType.String, Email);
            _db.AddParameter(cmd, "Uuid", DbType.String, Uuid);
            DataTable dt = CreateDataTable(cmd);
            return dt;
        }

        /// <summary>
        /// Lay thông tin device theo Uuid
        /// </summary>
        /// <param name="Uuid"></param>
        /// <returns></returns>
        public DataTable VNA_Select_Devices_by_Uuid(string Uuid)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Select_Devices_by_Uuid", true);
            _db.AddParameter(cmd, "Uuid", DbType.String, Uuid);
            DataTable dt = CreateDataTable(cmd);
            return dt;
        }

        /// <summary>
        /// Huy kich hoat thiet bi theo email
        /// </summary>
        /// <param name="Email"></param>
        public void VNA_Devices_DeActive(string Email)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Devices_DeActive", true);
            _db.AddParameter(cmd, "Email", DbType.String, Email);
            CreateDataTable(cmd);
        }

        /// <summary>
        /// Log Download
        /// </summary>
        /// <param name="Username"></param>
        /// <param name="AppID"></param>
        /// <param name="Email"></param>
        /// <param name="Uuid"></param>
        /// <param name="BinaryBuildTime"></param>
        /// <param name="ExpirationDate"></param>
        /// <param name="ipadName"></param>
        /// <param name="deviceToken"></param>
        public void VNA_Insert_LogDownload(string Username, string AppID, string Email, string Uuid, string BinaryBuildTime,
            DateTime ExpirationDate, string ipadName, string deviceToken)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Insert_LogDownload", true);
            _db.AddParameter(cmd, "Email", DbType.String, Email);
            _db.AddParameter(cmd, "Username", DbType.String, Username);
            _db.AddParameter(cmd, "AppID", DbType.String, AppID);
            _db.AddParameter(cmd, "Uuid", DbType.String, Uuid);
            _db.AddParameter(cmd, "BinaryBuildTime", DbType.String, BinaryBuildTime);
            _db.AddParameter(cmd, "ExpirationDate", DbType.DateTime, ExpirationDate);
            _db.AddParameter(cmd, "iPadName", DbType.String, ipadName);
            _db.AddParameter(cmd, "deviceToken", DbType.String, deviceToken);
            CreateDataTable(cmd);
        }

        public DataTable VNA_Report_LogSync(int Day, string Email, string Uuid)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Report_LogSync", true);
            _db.AddParameter(cmd, "Email", DbType.String, Email);
            _db.AddParameter(cmd, "Uuid", DbType.String, Uuid);
            _db.AddParameter(cmd, "Day", DbType.Int32, Day);
          
            return CreateDataTable(cmd);
        }

        /// <summary>
        /// Lay tat ca device da kich hoat hoac chua kich hoat
        /// </summary>
        /// <returns></returns>
        public DataTable VNA_Select_Devices_All()
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Select_Devices_All_by_Email", true);
            _db.AddParameter(cmd, "Email", DbType.String, "");
            return CreateDataTable(cmd);
        }

        /// <summary>
        /// Danh sach email admin theo tung appid
        /// </summary>
        /// <param name="appId"></param>
        /// <returns></returns>
        public DataTable VNA_Select_EmailAdmin(string appId)
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Select_EmailAdmin", true);
            _db.AddParameter(cmd, "AppID", DbType.String, appId);
            return CreateDataTable(cmd);
        }

        /// <summary>
        /// Lay danh sach cac email supper
        /// </summary>
        /// <returns></returns>
        public DataTable VNA_Select_Emails()
        {
            IDbCommand cmd = _db.CreateCommand("VNA_Select_Emails", true);
            return CreateDataTable(cmd);
        }
       
    }
} // End of namespace



